﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MLRobotic.Tower.Detection;

namespace MLRobotic.Tower.Forms
{
    public partial class CalibrateControl : UserControl
    {
        private TargetColor _targetColor;
        public TargetColor TargetColor { get { return _targetColor; } set { _targetColor = value; UpdateDisplay(); } }

        public CalibrateControl()
        {
            InitializeComponent();
            LoadHslSample();
        }

        private void LoadHslSample()
        {
            try
            {
                string directory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetCallingAssembly().GetName().CodeBase);
                string file = System.IO.Path.Combine(directory, @"Assets\hsl_sample.jpg");
                pictureSample.Image = new Bitmap(file);
            }
            catch (Exception) { }
        }
        
        private void UpdateDisplay()
        {
            UpdateHTrackBar(trackBarH, labelH, _targetColor.Hue);
            UpdateHTrackBar(trackBarHdelta, labelHdelta, _targetColor.DeltaHue);

            UpdateSLTrackBar(trackBarSmin, labelSmin, _targetColor.MinSaturation);
            UpdateSLTrackBar(trackBarSmax, labelSmax, _targetColor.MaxSaturation);

            UpdateSLTrackBar(trackBarLmin, labelLmin, _targetColor.MinBrightness);
            UpdateSLTrackBar(trackBarLmax, labelLmax, _targetColor.MaxBrightness);
        }

        private void UpdateHTrackBar(TrackBar track, Label label, double value)
        {
            int val = (int)(value / 10);
            track.Value = val;
            label.Text = ((int)value).ToString();
        }

        private void UpdateSLTrackBar(TrackBar track, Label label, double value)
        {
            int val = (int)(value * 10);
            track.Value = val;
            label.Text = value.ToString("F");
        }

        private double UpdateHTargetColor(TrackBar track, Label label)
        {
            int val = track.Value * 10;
            label.Text = val.ToString();
            return (double)val;
        }

        private double UpdateSLTargetColor(TrackBar track, Label label)
        {
            double val = ((double)track.Value) / 10;
            label.Text = val.ToString("F");
            return val;
        }

        private void trackBarH_ValueChanged(object sender, EventArgs e)
        {
            _targetColor.Hue = UpdateHTargetColor(sender as TrackBar, labelH);
        }

        private void trackBarHdelta_ValueChanged(object sender, EventArgs e)
        {
            _targetColor.DeltaHue = UpdateHTargetColor(sender as TrackBar, labelHdelta);
        }

        private void trackBarSmin_ValueChanged(object sender, EventArgs e)
        {
            _targetColor.MinSaturation = UpdateSLTargetColor(sender as TrackBar, labelSmin);
        }

        private void trackBarSmax_ValueChanged(object sender, EventArgs e)
        {
            _targetColor.MaxSaturation = UpdateSLTargetColor(sender as TrackBar, labelSmax);
        }

        private void trackBarLmin_ValueChanged(object sender, EventArgs e)
        {
            _targetColor.MinBrightness = UpdateSLTargetColor(sender as TrackBar, labelLmin);
        }

        private void trackBarLmax_ValueChanged(object sender, EventArgs e)
        {
            _targetColor.MaxBrightness = UpdateSLTargetColor(sender as TrackBar, labelLmax);
        }
    }
}
